/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kim.rssreader.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndContent;
import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndContentImpl;
import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndEntry;
import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndFeed;
import com.kim.rssreader.R;
import com.kim.rssreader.db.DBDAO;
import com.kim.rssreader.utils.DateUtil;
import com.kim.rssreader.utils.HtmlTagFilter;

/**
 * 
 * @author Administrator
 * 
 */
public class SyndFeedListAdapter extends BaseAdapter {

	public static final String TAG = "SyndFeedListAdapter";

	private SyndFeed syndFeed;
	private final LayoutInflater layoutInflater;
	private DBDAO dbdao;

	public SyndFeedListAdapter(Context context, SyndFeed feed, DBDAO dbdao) {
		this.syndFeed = feed;
		this.dbdao = dbdao;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public void resetData(SyndFeed feed) {
		this.syndFeed = feed;
	}

	public int getCount() {
		return syndFeed != null ? syndFeed.getEntries().size() : 0;
	}

	public SyndEntry getItem(int position) {
		return (SyndEntry) syndFeed.getEntries().get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		SyndEntry syndEntry = getItem(position);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.rss_list_item,
					parent, false);
			holder = new ViewHolder();
			holder.title = (TextView) convertView
					.findViewById(R.id.synd_feed_title);
			holder.date = (TextView) convertView
					.findViewById(R.id.synd_feed_date);
			holder.desc = (TextView) convertView
					.findViewById(R.id.synd_feed_description);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setTextColor(Color.BLACK);
		holder.date.setTextColor(Color.BLACK);
		holder.desc.setTextColor(Color.BLACK);

//		@SuppressWarnings("unchecked")
//		List<Object> contents = syndEntry.getContents();
//		if (contents != null && contents.size() > 0) {
//			Object object = contents.get(0);
//			if (object instanceof SyndContentImpl) {
//			} else {
//				String content = (String) contents.get(0);
//				if (content != null && !"".equals(content)) {
//					holder.title.setTextColor(Color.GRAY);
//					holder.date.setTextColor(Color.GRAY);
//					holder.desc.setTextColor(Color.GRAY);
//				}
//			}
//		}

		/**
		 * 显示已读状态
		 */
		if (dbdao.getReadState(syndEntry.getLink())) {
			holder.title.setTextColor(Color.GRAY);
			holder.date.setTextColor(Color.GRAY);
			holder.desc.setTextColor(Color.GRAY);
		}

		if (syndEntry != null) {
			String title = syndEntry.getTitle();
			title = HtmlTagFilter.filter(title, true);
			// Log.i(TAG, "title::"+title);
			holder.title.setText(title);

			holder.date.setText(DateUtil.parseDate2Str(syndEntry
					.getPublishedDate()));

			SyndContent description = syndEntry.getDescription();
			String content = "";
			if (description != null) {
				content = description.getValue();
				content = HtmlTagFilter.filter(content, false);
				holder.desc.setText(content);
			}
		}

		return convertView;
	}

	private class ViewHolder {
		TextView title;
		TextView date;
		TextView desc;
	}

}
